
clear all
cap log close

log using "${logfiles}f04_match_causal_defendant.log", replace

sysdir set PERSONAL "W:\ado"
set scheme kailascheme


*Define dummie used in the eventstudies
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
*define fixed effects 
global fe = "baseid time year age_time match_id1" 
*define clustering
global cluster = "shnro"


capture program drop eventStudyGraphs
program define eventStudyGraphs
	args a b c d
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace coef_est = 0 if t == - 1
	replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	


	
	*Main regression for top guys 
	reghdfe `a'  treatPost ,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time since Violence") ), ///
		 legend( order( 2 "Perpetrators" ) rows(2) position(7) ring(0) ) ///
			
	gr export "${results}\reg_`a'_defendant_draft.pdf", replace
	gr export "${results}\reg_`a'_defendant_draft.eps", replace
	
	* Save estimates 
	keep coef* se_est uC* lC* obs* t
	drop if missing(t)
	gen outcome = "`a'"
	restore
	}
end


use "${dataout}allmatches_allyears_matchpast5", clear
gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys match_id1 year_event: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1


destring suspect_sex, replace 
destring plaintiff_sex, replace
	
gen mm=suspect_sex==1 & plaintiff_sex==1
gen mf=suspect_sex==1 & plaintiff_sex==2
	
sort match_id1 year_event time shnro 
bys match_id1 year_event time: ereplace mf=max(mf)
bys match_id1 year_event time: ereplace mm=max(mm)



gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

gen age_time=age+time


*Time displacement dummies
g dpl_5=time==-5 & treat==1
g dpl_4=time==-4 & treat==1
g dpl_3=time==-3 & treat==1
g dpl_2=time==-2 & treat==1
g dpl_1=time==-1 & treat==1
g dpl_0=time==0 & treat==1
gen dpl1=time==1 & treat==1
gen dpl2=time==2 & treat==1
gen dpl3=time==3 & treat==1
gen dpl4=time==4 & treat==1
gen dpl5=time==5 & treat==1

gen treatPost= treat==1 & time>0



* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)



* earnings 
sort shnro year_event time 
gen prior_earnings1=realAllEarn if time==-1 
gen prior_earnings2=realAllEarn if time==-2 
gen prior_earnings3=realAllEarn if time==-3

bysort shnro year_event: ereplace  prior_earnings1=mean( prior_earnings1)
bysort shnro year_event: ereplace  prior_earnings2=mean( prior_earnings2)
bysort shnro year_event: ereplace  prior_earnings3=mean( prior_earnings3)

egen prior_earnings=rmean (prior_earnings1 prior_earnings2 prior_earnings3)

gen share_earning=realAllEarn/prior_earnings

sum share_earning, d 
replace share_earning=10 if share_earning>10




sort match_id1 time shnro 
bys match_id1 time: ereplace plaintiff_sex=max(plaintiff_sex)





preserve 
*1) male on female crimes 
keep if mf==1 
rename employed mf_employed
rename realAllEarn mf_realAllEarn
* employment 
eventStudyGraphs "mf_employed" "-0.2(0.05)0.05" "Employed" 
rename share_earning mf_share_earning
eventStudyGraphs "mf_share_earning" "-0.4(0.1)0.2" "Earnings"
restore 


preserve 
*1) male on male crimes 
keep if mm==1
rename employed mm_employed
rename realAllEarn mm_realAllEarn
* employment 
eventStudyGraphs "mm_employed" "-0.2(0.05)0.05" "Employed" 
rename share_earning mm_share_earning
eventStudyGraphs "mm_share_earning" "-0.4(0.1)0.2" "Earnings"
restore 



